package com.ww.java8.fp;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;

/**
 * 流的建造者模式
 * @author wanggw
 * 2023年3月10日 下午7:25:48
 */
public class FileToWordsBuilder {

	Stream.Builder<String> builder = Stream.builder();
	
	public FileToWordsBuilder(String filePath) throws Exception {
		Files.lines(Paths.get(filePath))
			.skip(1)
			.forEach(line -> {
				for (String w : line.split("[ .?,]+")) {
					builder.add(w);
				}
			});
	}
	
	Stream<String> stream() {
		return builder.build();
	}
	
	public static void main(String[] args) throws Exception {
		new FileToWordsBuilder("Cheese.dat")
			.stream()
			.limit(7)
			.map(w -> w + " ")
			.forEach(System.out::print);
	}
}
